﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;

namespace NetCoreCMS.Web.Areas.Web.Controllers
{
    public class NewsController : BaseController
    {
        public IArticleService _article { get; set; }
        public IArticle_categoryService _article_category { get; set; }
        [ResponseCache(Duration = 600)]
        public IActionResult Index()
        {
            ViewBag.focusList = _article.GetList(8, "where status=0 and channel_id=1 and is_slide=1 and img_url<>''", "sort_id asc,id desc");
            ViewBag.newsList = _article.GetList(8, "where status=0 and channel_id=1 and img_url<>''", "add_time desc,id desc");
            ViewBag.redList = _article.GetList(5, "where status=0 and channel_id=1 and is_red=1 and img_url<>''", "id desc");
            ViewBag.hotList = _article.GetList(10, "where status=0 and channel_id=1", "click desc,id desc");
            return View();
        }
        public int page = 1;         //当前页码
        public int totalcount = 0;   //OUT数据总数
        public IActionResult List(int id)
        {
            var cate = _article_category.GetModel(id);
            if (cate == null)
            {
                return ToError("分类不存在！");
            }
            int.TryParse(Request.Query["page"], out page);
            page = page > 0 ? page : 1;
            ViewBag.Page = page;
            string strwhere = "where status=0 and channel_id=1 and category_id="+cate.id;
            var list = _article.GetList(page, 50, strwhere, "add_time desc,id desc");
            totalcount = _article.RecordCount(strwhere);
            string pageUrl = Utils.CombUrlTxt("/Web/News/List/"+id, "page={0}", "__id__");
            ViewBag.pagelist = Utils.OutPageList(50, page, totalcount, pageUrl, 8);
            ViewBag.cate = cate;
            return View(list);
        }
        [ResponseCache(Duration = 600)]
        public IActionResult Show(int id)
        {
            var model = _article.GetModel(id);
            ViewBag.relNews = _article.GetList(5, "where category_id=" + model.category_id + " and is_red=1 and id<>" + model.id, "id desc");
            ViewData["model"] = model;
            return View(model);
        }
        [ResponseCache(Duration = 600)]
        public IActionResult Content(int id)
        {
            var model = _article.GetModel(id);
            ViewBag.category_title = _article_category.GetModel(model.category_id).title;
            return View(model);
        }
    }
}